package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.OrderListDto;
import com.igeekhome.mapper.OrderListDtoMapper;
import com.igeekhome.service.IOrderListService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @ClassName OrderListServiceImpl
 * @Author admin(汪璇)
 * @Date 2020/1/19 18:39
 * NOSCAN = "1"
 * #已扫描
 * SAN = "2"
 * #已派件
 * DIST_ORDER = "3"
 * #已签收
 * SIGN_ORDER = "4"
 * @Description TODO
 */
@Service(value = "OrderListService")
public class OrderListServiceImpl implements IOrderListService {
    @Autowired
    private OrderListDtoMapper orderListDtoMapper;
    
    @Value("${SAN}")
    private String SAN;
    
    @Value("${DIST_ORDER}")
    private String DIST_ORDER;
    
    @Value("${SIGN_ORDER}")
    private String SIGN_ORDER;
    
    //订单查询列表
    @Override
    public TableResultBean orderList(Integer page, Integer limit, OrderListDto orderListDto) {
        List<OrderListDto> orderListDtos = returnList(page, limit, orderListDto, null);
        
        //获取集合总条数
        long total = new PageInfo<OrderListDto>(orderListDtos).getTotal();
        
        return TableResultBean.ok(total,orderListDtos);
    }
    
    //订单分配列表
    @Override
    public TableResultBean ditOrderList(Integer page, Integer limit, OrderListDto orderListDto) {
        List<OrderListDto> orderListDtos = returnList(page, limit, orderListDto, SAN);
        //获取集合总条数
        long total = new PageInfo<OrderListDto>(orderListDtos).getTotal();
        return TableResultBean.ok(total,orderListDtos);
    }
    
    
    //订单分配操作
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean ditOrder(Integer staffId, Integer[] orders) {
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            for (Integer order : orders) {
                orderListDtoMapper.updateByStaffId(order,staffId);
                orderListDtoMapper.updateByStatus(order,DIST_ORDER);
            }
            return ResultBean.ok();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }
    
    
    //签收录入列表
    @Override
    public TableResultBean signList(Integer page, Integer limit, OrderListDto orderListDto) {
        List<OrderListDto> orderListDtos = returnList(page, limit, orderListDto, DIST_ORDER);
        //获取集合总条数
        long total = new PageInfo<OrderListDto>(orderListDtos).getTotal();
        return TableResultBean.ok(total,orderListDtos);
    }
    
    
    //已签收列表
    @Override
    public TableResultBean sign(Integer page, Integer limit, OrderListDto orderListDto) {
        List<OrderListDto> orderListDtos = returnList(page, limit, orderListDto, SIGN_ORDER);
        //获取集合总条数
        long total = new PageInfo<OrderListDto>(orderListDtos).getTotal();
        return TableResultBean.ok(total,orderListDtos);
    }
    
    /**
    * @Description订单签收操作
    * @Param [orders]
    * @Return com.igeekhome.util.ResultBean
    **/
    @Override
    public ResultBean signOrder(Integer[] orders) {
        for (Integer order : orders) {
            orderListDtoMapper.updateByStatus(order,SIGN_ORDER);
        }
        return ResultBean.ok();
    }
    
    /**
    * @Description 返回查询列表的公用方法
    * @Param [page, limit, orderListDto, status]
    * @Return java.util.List<com.igeekhome.dto.OrderListDto>
    **/
    public List<OrderListDto> returnList(Integer page, Integer limit, OrderListDto orderListDto,String status){
        orderListDto.setCatalogueState(status);
        //开启分页
        PageHelper.startPage(page,limit);
    
        //获取订单集合
        List<OrderListDto> orderListDtos = orderListDtoMapper.selectByCondition(orderListDto);
    
        //拆分详细地址为省,市,区,地址
        for (OrderListDto dto : orderListDtos) {
            //拆分寄件人详细地址
            String[] site1 = CommonUtil.addressToList(dto.getSendAddress());
            //拆分收件人详细地址
            String[] site2 = CommonUtil.addressToList(dto.getConsigneeAddress());
        
            if (site1.length == 4) {
                dto.setProvince(site1[0]).setCity(site1[1]).setArea(site1[2]).setAddress(site1[3]);
            }
        
            if (site2.length == 4) {
                dto.setRecipientProvince(site2[0]).setRecipientCity(site2[1]).setRecipientArea(site2[2]).setRecipientAddress(site2[3]);
            }
        }
        return orderListDtos;
    }
}
